package cn.jyc.ssm03.utils;

import cn.jyc.ssm03.pojo.Log;
import cn.jyc.ssm03.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Resource
public class LogFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
        AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
        factory.autowireBean(this);
//        在servlet中使用SpringMVC的Service层接口
//        参考：https://www.jianshu.com/p/499089a71cb7
    }

    @Autowired
    LogService logS;

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
//        System.out.println(logS);
//        System.out.println("=======================过滤器参数=========================");
        HttpServletRequest req2 = (HttpServletRequest) req;
        String uri = req2.getRequestURI();
//        System.out.println(uri);
        String method = req2.getMethod();
//        System.out.println(method);
        String remoteAddr = req2.getRemoteAddr();
//        System.out.println(remoteAddr);
        HttpSession session = req2.getSession();
        String unm = (String) session.getAttribute("unm");
//        System.out.println(unm);
        String queryString = req2.getQueryString();
//        System.out.println(queryString);
//        java.util.Date date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
//        System.out.println(date);

//        LocalDateTime转Date
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Instant instant = zonedDateTime.toInstant();
        Date date2 = Date.from(instant);
//        System.out.println(date2);
//        System.out.println("=========================================================");
        Log log = new Log(unm, remoteAddr, date2, method + ":" + uri, queryString);
//        System.out.println(log);

        Set<String> qwer = new HashSet<>();
        qwer.add(".png");
        qwer.add(".gif");
        qwer.add(".css");
        qwer.add(".js");
        qwer.add(".jpg");

        int i = uri.lastIndexOf(".");
        if (i==-1){
            logS.addLog(log);
        } else {
            String substring = uri.substring(i);
            if (!qwer.contains(substring)) {
                logS.addLog(log);
            }
        }
        chain.doFilter(req, resp);
    }
}
